package com.wish.g1.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.web.page.TableDataInfo;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.finance.entity.Product;
import com.wish.g1.finance.entity.RentInstallment;
import com.wish.g1.finance.entity.SfLoan;
import com.wish.g1.finance.entity.query.RentInstallmentQuery;
import com.wish.g1.finance.entity.vo.RentInstallmentVo;
import com.wish.g1.finance.service.IFactoringService;
import com.wish.g1.finance.service.IRentInstallmentService;
import com.wish.g1.finance.service.ISfLoanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

import static com.wish.g1.common.core.utils.PageUtils.startPage;

/**
 * <p>
 * 房租分期申请 前端控制器
 * </p>
 *
 * @author wxh
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/installment")
public class RentInstallmentController extends BaseController {
    @Autowired
    private IRentInstallmentService rentInstallmentService;
    @Autowired
    private ISfLoanService sfLoanService;



    /**
     * 查询房租分期申请列表
     */
//    @RequiresPermissions("finance:installment:list")
//    @GetMapping("/list")
//    public TableDataInfo list(RentInstallmentQuery rentInstallmentQuery)
//    {
//        startPage();
//        List<RentInstallmentVo> list = rentInstallmentService.listRent(rentInstallmentQuery);
//        return getDataTable(list);
//    }
    @RequiresPermissions("finance:installment:list")
    @GetMapping("/list")
    public TableDataInfo list(RentInstallmentVo rentInstallmentVo)
    {
        startPage();
        List<RentInstallmentVo> list = rentInstallmentService.searchRentInstallmentPage(rentInstallmentVo);
        return getDataTable(list);
    }


    @RequestMapping("rentingAuditView")
    public AjaxResult rentingAuditView(RentInstallmentVo rentInstallmentVo){

        ModelAndView mv =new ModelAndView();
        Long business=null;
        // 金融产品通过jrProductType判断 跳转不同页面：   1:租房分期  2:收房贷  3:应收租约保理
        if(rentInstallmentVo.getJrProductType()==1){

        }else if(rentInstallmentVo.getJrProductType()==2){
            //获取收房贷详细信息
            SfLoan sfLoan = sfLoanService.selectSfLoanDetails(Long.valueOf(rentInstallmentVo.getId()));
            System.out.println("1++++++++"+sfLoan);
            BigDecimal loanAmount = sfLoan.getLoanAmount();
            //贷款利息
            BigDecimal divide = loanAmount.divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply = divide.multiply(sfLoan.getRate());
            sfLoan.setReserve1(multiply);
            //一次性服务费
            BigDecimal divide1 = loanAmount.divide(BigDecimal.valueOf(100),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal multiply1 = divide.multiply(sfLoan.getServiceFee());
            sfLoan.setReserve2( multiply1);
            //周期还款金额
            BigDecimal divide2 = multiply.divide(BigDecimal.valueOf(sfLoan.getMonthLimit()), 2, BigDecimal.ROUND_HALF_UP);
            BigDecimal divide3= multiply1.divide(BigDecimal.valueOf(sfLoan.getMonthLimit()),2,BigDecimal.ROUND_HALF_UP);
            BigDecimal add = divide2.add(divide3);
            sfLoan.setReserve3(String.valueOf(add));
            //获取收房信息
//            TSfContract =
            return  AjaxResult.success(sfLoan);
        }else {

        }

        return  AjaxResult.success(mv);
    }

}
